<?php

class Output_Exception extends CMS_Exception {}
class Output_Invalid_Exception extends CMS_Exception {}

interface IOutput {
	public static function Load_Output();
	public function Display($template);
	public function Template($tplname);
}

abstract class Output implements IOutput {

	private static $instance;

	public static function getInstance(){
		if(!isset(Output::$instance)){
			throw new Output_Invalid_Exception();
		}
		return Output::$instance;
	}

	private static function Load_Output_Helper($name){
		$module = Module::Get($name);
		$class = $module->Load_Section('Output');
		Output::$instance = call_user_func(array($class, 'Load_Output'), $name);
		if(!(Output::$instance instanceof Output)){
			throw new Output_Invalid_Exception(Output::$instance);
		}
		return Output::$instance;
	}

	public static function Load_Output($name = null){
		if(isset($name)){
			throw new Output_Invalid_Exception();
		}	
		try {
			if(isset($_GET['output'])){
				return Output::Load_Output_Helper($_GET['output']);
			}
		} catch (Exception $e){

		}

		return Output::Load_Output_Helper(Site::getInstance()->getOutput());
		
	}

}
